#!/usr/bin/env php
<?php

$files = array(
  __DIR__ . '/../vendor/autoload.php',
  __DIR__ . '/../../../autoload.php',
  __DIR__ . '/../Mf2/Parser.php'
);

foreach ($files as $file) {
	if (file_exists($file)) {
		require $file;
		define('COMPOSER_INSTALL_LOCATION', $file);
		break;
	}
}

if (!defined('COMPOSER_INSTALL_LOCATION')) {
	die('A composer /vendor/autoload.php file could not be found. You need to install composer: https://getcomposer.org/download');
}

$url = $argv[1];

if (empty($url)) {
	die("Please provide a URL to fetch+parse, e.g. bin/fetch-mf2 waterpigs.co.uk/notes");
}

if (!file_exists($url)) {
	if (!preg_match('|https?://|', $url)) {
		$url = 'http://' . $url;
	}
}

$result = Mf2\fetch($url);

if (defined('JSON_PRETTY_PRINT')) {
	echo json_encode($result, JSON_PRETTY_PRINT);
} else {
	echo json_encode($result);
}
